package us.mooresr.webtroopmanager.client.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import us.mooresr.webtroopmanager.client.place.VehicleListPlace;
import us.mooresr.webtroopmanager.client.ui.EditPersonViewImpl.EditPersonUiBinder;
import us.mooresr.webtroopmanager.shared.PersonSummary;
import us.mooresr.webtroopmanager.shared.Vehicle;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


public class EditVehicleViewImpl extends Composite implements EditVehicleView {
	private Presenter presenter;
	
	private Long vehicleId;
	
	private Long ownerId = null;
	
	// Used for the case where we know which index is selected and need to know what person that is
	private Map<Integer, PersonSummary> comboBoxMap;
	
	// Used for the case where we have a person's ID and need to select that person in the combo list
	private Map<Long, Integer> comboBoxReverseMap;
	
	private static EditVehicleUiBinder uiBinder = GWT.create(EditVehicleUiBinder.class);
	
	interface EditVehicleUiBinder extends UiBinder<Widget, EditVehicleViewImpl> {
		
	}
	
	@UiField
	TextBox yearTextBox;
	
	@UiField
	TextBox makeTextBox;
	
	@UiField
	TextBox modelTextBox;
	
	@UiField
	TextBox seatbeltsTextBox;
	
	@UiField
	TextBox liabPerPersonTextBox;
	
	@UiField
	TextBox liabPerAccidentTextBox;
	
	@UiField
	TextBox propertyDamageTextBox;
	
	@UiField
	ListBox ownerListBox;

	@UiHandler("okButton")
	void onOkClick(ClickEvent event) {
		// TODO:  Validate the data
		
		/*
		 * Vehicle(Long id, String make, String model, Integer modelYear,
		 * Integer seatbelts, Integer liabilityPerPerson,
		 * Integer liabilityPerAccident, Integer propertyDamage, Long owner)
		 */
		
		Integer iModelYear;
		try {
			iModelYear = Integer.parseInt(yearTextBox.getValue());
		} catch (Exception e) {
			iModelYear = 0;
		}
		
		Integer iSeatbelts;
		try {
			iSeatbelts = Integer.parseInt(seatbeltsTextBox.getValue());
		} catch (Exception e) {
			iSeatbelts = 0;
		}
		
		Integer iLiabPerPerson;
		try {
			iLiabPerPerson = Integer.parseInt(liabPerPersonTextBox.getValue());
		} catch (Exception e) {
			iLiabPerPerson = 0;
		}
		
		Integer iLiabPerAccident;
		try {
			iLiabPerAccident = Integer.parseInt(liabPerAccidentTextBox.getValue());
		} catch (Exception e) {
			iLiabPerAccident = 0;
		}
		
		Integer iPropertyDamage;
		try {
			iPropertyDamage = Integer.parseInt(propertyDamageTextBox.getValue());
		} catch (Exception e) {
			iPropertyDamage = 0;
		}
		
		Integer ownerIndex = ownerListBox.getSelectedIndex();
		if (ownerIndex == 0) {
			ownerId = null;
		} else {
			PersonSummary asstLeader = comboBoxMap.get(ownerIndex);
			    ownerId = asstLeader.getId();
		}
		
		Vehicle v = new Vehicle(vehicleId, makeTextBox.getValue(), modelTextBox.getValue(),
				iModelYear, iSeatbelts, iLiabPerPerson, iLiabPerAccident, iPropertyDamage,
				ownerId);
		
		presenter.addVehicle(v);
		presenter.goTo(new VehicleListPlace());
	}
	
	@UiHandler("cancelButton")
	void onCancelClick(ClickEvent event) {
		presenter.goTo(new VehicleListPlace());
	}
	
	
	public EditVehicleViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		comboBoxMap = new HashMap<Integer, PersonSummary>();
		comboBoxReverseMap = new HashMap<Long, Integer>();
	}

	@Override
	public void setComboBoxes(List<PersonSummary> people) {
		comboBoxMap.clear();
		comboBoxReverseMap.clear();
		ownerListBox.clear();
		
		// Fill in the combo boxes for the leaders
		ownerListBox.insertItem("None", 0);
		comboBoxMap.put(0, null);
		ownerListBox.setSelectedIndex(0);
		int i = 1;
		for (PersonSummary ps : people) {
			ownerListBox.insertItem(ps.toString(), i);
			if (ps.getId().equals(ownerId)) {
				ownerListBox.setSelectedIndex(i);
			}
					
			comboBoxMap.put(i, ps);			
			comboBoxReverseMap.put(ps.getId(), i);
			++i;
		}
		
	}

	@Override
	public void setData(Vehicle vehicle) {
		if (vehicle == null) {
			vehicleId = null;
			yearTextBox.setValue("");
			makeTextBox.setValue("");
			modelTextBox.setValue("");
			seatbeltsTextBox.setValue("");
			liabPerPersonTextBox.setValue("");
			liabPerAccidentTextBox.setValue("");
			propertyDamageTextBox.setValue("");
			ownerId = null;
		} else {
			vehicleId = vehicle.getId();
			yearTextBox.setValue(vehicle.getModelYear().toString());
			makeTextBox.setValue(vehicle.getMake());
			modelTextBox.setValue(vehicle.getModel());
			seatbeltsTextBox.setValue(vehicle.getSeatbelts().toString());
			liabPerPersonTextBox.setValue(vehicle.getLiabilityPerPerson().toString());
			liabPerAccidentTextBox.setValue(vehicle.getLiabilityPerAccident().toString());
			propertyDamageTextBox.setValue(vehicle.getPropertyDamage().toString());
			ownerId = vehicle.getOwner();
		}
		
		// Select the correct entries for owner
		// Due to the asynchronous nature of the database calls the combo boxes
		// may not be filled in by the time we get the data.  So if we can't
		// find the indexes in the reverse map we just skip this part, they'll
		// get selected when the combo boxes get filled in
		
		
		Integer index;

		index = comboBoxReverseMap.get(ownerId);
		
		if (index != null) {
			ownerListBox.setSelectedIndex(index);
		} else {
			// ownerId is null, so we have no owner yet
			// if the combo box has been populated then "None" will always be index 0
			if (comboBoxReverseMap.size() != 0) {
				ownerListBox.setSelectedIndex(0);
			}
		}
		
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
	}

}
